package com.colorful.insuranceproject.commonActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.SendVerifyCodeActivity;
import com.colorful.insuranceproject.bean.VerifyBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.UserRequest;
import com.lovely3x.common.utils.CommonUtils;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/17.
 * <p>
 * 忘记密码
 */
public class ForgotPasswordActivity extends SendVerifyCodeActivity implements TextWatcher {

    private static final int REQUEST_CODE = 10000;
    private UserRequest userRequest;

    private static final int GET_VERIFY_CODE_URL = 0X1;

    private CountDownDesc mCountDownDesc;

    @Bind(R.id.et_phone)
    EditText etPhone;

    @Bind(R.id.et_verifyCode)
    EditText etVerifyCode;

    @Bind(R.id.tv_set)
    TextView tvSet;

    @Bind(R.id.tv_get_verifyCode_by_forgot)
    TextView tvGetVerifyCode;

    VerifyBean vb;

    @Override
    protected int getContentView() {
        return R.layout.activity_forgot_pwd;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.forgot_pwd));
    }

    @Override
    protected void onViewInitialized() {
        userRequest = new UserRequest(getHandler());
        etVerifyCode.addTextChangedListener(this);
        mCountDownDesc = new CountDownDesc(tvGetVerifyCode.getId(), DEFAULT_COUNT_DOWN_TIME, 100);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_VERIFY_CODE_URL:
                if (response.isSuccessful) {
                    vb = (VerifyBean) response.obj;
                    showToast(getString(R.string.verifyCode_send_success));
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                    sendVerifyCodeFailure(mCountDownDesc);
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 页面监听处理
     */
    @OnClick({R.id.tv_set, R.id.tv_get_verifyCode_by_forgot})
    void OnClicked(View view) {
        String phone = etPhone.getText().toString().trim();
        String code = etVerifyCode.getText().toString().trim();
        switch (view.getId()) {
            //  下一步
            case R.id.tv_set:
                if (phone.length() != 11 && code.length() != 4) {
                    showToast(getString(R.string.please_compleate_infomation));
                } else {
                    vb.setResult(phone);
                    vb.setCode(code);
                    launchActivityForResult(SetPasswordActivity.class, REQUEST_CODE, SetPasswordActivity.EXTRA_MESSAGE_ID, vb);
                }
                break;
            //  获取验证码
            case R.id.tv_get_verifyCode_by_forgot:
                if (CommonUtils.isMobilePhone(phone)) {
                    sendVerifyCode(mCountDownDesc.reset());
                    userRequest.getVerifyCodeForgot(phone, GET_VERIFY_CODE_URL);
                } else {
                    showToast(getString(R.string.please_enter_the_correct_phone_number));
                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            finish();
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        String phone = etPhone.getText().toString().trim();
        String code = etVerifyCode.getText().toString().trim();
        if (s.length() < 4) {
            tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
        } else {
            if (phone.length() == 11 && code.length() == 4) {
                tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_blue_such_as_login));
            }
        }
    }
}
